<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>作用域插槽</title>
</head>

<body>

  <div id="app">
    <cpn></cpn>

    <cpn>
      <!-- template目的就是为了获取子组件中的pLanguages -->
      <template slot-scope="slot">
        <span v-for="item in slot.data">{{ item }} - </span>
      </template>
    </cpn>

    <!-- 上面的多了个 - 符号结尾 -->
    <cpn>
      <!-- template目的就是为了获取子组件中的pLanguages -->
      <template slot-scope="slot">
        <span>{{ slot.data.join(' - ') }}</span>
      </template>
    </cpn>
  </div>

  <template id="cpn">
    <div>
      <!-- 插槽: 默认ul>li方式排列, 可以替换 -->
      <!-- :data 变量取名 可以随意取名 -->
      <slot :data="pLanguages">
        <ul>
          <li v-for="item in pLanguages">{{ item }}</li>
        </ul>
      </slot>
    </div>
  </template>

  <script src="../../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        message: 'hello'
      },

      components: {
        cpn: {
          template: '#cpn',
          data() {
            return {
              pLanguages: [
                'JavaScript',
                'Java',
                'Python',
                'Go',
                'C++'
              ]
            }
          }
        }
      }
    })
  </script>
</body>

</html>